/**
 * Created by lesonli on 2016/11/17.
 */
import * as types from '../types'
import api from '../../api/modules/config'
import {
    getList,
    addInstitution,
    updateInstitution,
    switchState
} from '../../api/modules/institution'

const state = {
    institution_list: [],
    total: 0,
    showLoading: false,
    state_list: [{id:0,name:'待审核'},{id:1,name:'正常'}],
    type_list: [{id:0,name:'意向机构'},{id:1,name:'正式机构'}]
}
var catch_page_list_data = {}
// getters
// actions
const actions = {
    get_institution_list({
            commit
        }, param) {
        commit(types.INSTITUTION_SHOW_LOADING)
        // if(param.project_id){
        getList(param).then(res => {
            if (res.data.res_code === 1) {
                commit(types.INSTITUTION_LIST_LOADED,
                    res.data.msg,
                );
            }
        })
    },
    add_institution({
            commit
        }, params) {
        commit(types.INSTITUTION_SHOW_LOADING);
        addInstitution(params).then(res => {
            if (res.data.res_code === 1) {
                params._fn();
                commit(types.INSTITUTION_ADDED, {
                    data: params,
                    result: res.data.msg
                });
            }
        })
    },
    update_institution({
            commit
        }, params) {
        commit(types.INSTITUTION_SHOW_LOADING);
        updateInstitution(params).then(res => {
            if (res.data.res_code === 1) {
                commit(types.INSTITUTION_UPDATED, params);
                params._fn();
            }
        })
    },
    update_insttution_state({
            commit
        }, param) {
        commit(types.INSTITUTION_SHOW_LOADING);
        switchState(param.id, param.value).then(res => {
            if (res.data.res_code === 1) {
                commit(types.INSTITUTION_UPDATED, param);
            }
        })
    },
    clear_institution({ commit }) {
        commit(types.INSTITUTION_CLEAR);
    }
}
// mutations
const mutations = {
    [types.INSTITUTION_SHOW_LOADING](state) {
        state.showLoading = true;
    },
    [types.INSTITUTION_CLEAR](state) {
        state.institution_list = [];
    },
    [types.INSTITUTION_LIST_LOADED](state, param) {
        //state.student_list = param.results;
        state.total = param.count;
        for(var i=0;i<param.length;i++)
        {
            param[i].id = param[i].institution_id;
        }
        state.institution_list = param;
        state.showLoading = false;

        state.isInited = true;
    },
    [types.INSTITUTION_ADDED](state, params) {
        state.institution_list.push({
            id: params.result,
            institution_id: params.result,
            contact: params.data.contact,
            job: params.data.job,
            phone: params.data.phone,
            email: params.data.email,
            dealer_id: params.data.dealer_id,
            dealer_company: params.data.dealer_company,
            sale_user_id: params.data.sale_user_id,
            sale_user_name: params.data.sale_user_name,
            province_id: params.data.province_id,
            branch_campus_count: params.data.branch_campus_count,
            main_project: params.data.main_project,
            teacher_count: params.data.teacher_count,
            type: params.data.type,
            state: params.data.state,
        });
        state.showLoading = false;
    },
    [types.INSTITUTION_UPDATED](state, params) {
        for (var i = 0; i < state.institution_list.length; i++) {
            if (state.institution_list[i].id === params.institution_id) {
                if (!params.hasOwnProperty('value')) {
                    for(var p in params)
                    {
                        state.institution_list[i][p] = params[p];
                    }
                }
                else {
                    state.institution_list[i].state = params.value;
                }
                break;
            }
        }
        state.showLoading = false;
    }
}

export default {
    state,
    actions,
    mutations
}
